package org.example.controller;

import org.example.annotation.CheckPermission;
import org.example.context.BaseContext;
import org.example.pojo.domain.Feedback;
import org.example.result.Result;
import org.example.service.FeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/feedback")
public class FeedbackController {

    @Autowired
    private FeedbackService feedbackService;

    /**根据展览ID获取所有反馈(所有角色)**/
    @CheckPermission({"visitor","curator","admin"})
    @GetMapping("/exhibition/{exhibitionId}")
    public Result listFeedbacks(@PathVariable Integer exhibitionId) {
        List<Feedback> feedbacks = feedbackService.listFeedbacksByArtifact(exhibitionId);
        return Result.success("展览ID("+exhibitionId+")获取所有反馈",feedbacks);
    }

    /**
     * 获取当前用户对指定展览的最新反馈
     * @param exhibitionId 展览 ID
     */
    @CheckPermission({"visitor"})
    @GetMapping("/latest/{exhibitionId}")
    public Result getLatestFeedback(@PathVariable Integer exhibitionId) {
        Integer userId = Integer.parseInt(BaseContext.getCurrentId());
        Feedback feedback = feedbackService.getLatestFeedback(userId, exhibitionId);
        if (feedback == null) {
            return Result.success("未找到任何反馈", null);
        }
        return Result.success("最新反馈查询成功", feedback);
    }


    /**根据反馈ID获取反馈详情(所有角色)**/
    @CheckPermission({"visitor","curator","admin"})
    @GetMapping("/{feedbackId}")
    public Result getFeedback(@PathVariable Integer feedbackId) {
        Feedback feedback = feedbackService.getFeedbackById(feedbackId);
        return Result.success("反馈ID("+feedbackId+")获取反馈详情",feedback);
    }

    /**提交反馈(visitor)**/
    @CheckPermission({"visitor"})
    @PostMapping("/add")
    public Result addFeedback(@RequestBody Feedback feedback) {
        feedbackService.addFeedback(feedback);
        return Result.success("反馈提交成功！");
    }

    /**删除反馈(visitor)**/
    @CheckPermission({"visitor"})
    @DeleteMapping("/delete/{feedbackId}")
    public Result deleteFeedback(@PathVariable Integer feedbackId) {
        feedbackService.deleteFeedback(feedbackId);
        return Result.success("反馈删除成功！");
    }

    /**更新反馈(visitor)**/
    @CheckPermission({"visitor"})
    @PutMapping("/update")
    public Result updateFeedback(@RequestBody Feedback feedback) {
        feedbackService.updateFeedback(feedback);
        return Result.success("反馈更新成功！");
    }
}
